package com.limespot.api.tree;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;

import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;

import com.limespot.api.User;
import com.limespot.api.misc.AbstractMain;

public class Main extends AbstractMain {
	
	public void realMain() {
		
		StatusBar sb = new StatusBar();
		JTree tree = new JTree();
		tree.setPreferredSize(new Dimension(300,600));
		final JPanel info = new JPanel(new BorderLayout());
		TreeModel model = new LimeSpotTreeModel(tree,getUser(),sb);
		tree.setModel(model);
		final JSplitPane sp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, tree, info);
		tree.addTreeSelectionListener(new TreeSelectionListener() {

			public void valueChanged(TreeSelectionEvent e) {
				UserTreeNode node = (UserTreeNode)e.getPath().getLastPathComponent();
				User user = node.getUser();
				sp.setRightComponent(new UserPanel(user));
			}
			
		});
		tree.setShowsRootHandles(true);
		
		JFrame f = new JFrame();



		Container c = f.getContentPane();
		c.setLayout(new BorderLayout());
		JScrollPane scrollPane = new JScrollPane(sp);
		c.add(scrollPane, BorderLayout.CENTER);
		c.add(sb, BorderLayout.SOUTH);
		c.setPreferredSize(new Dimension(600,600));
		
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.pack();
		f.setVisible(true);
		
		tree.expandPath(new TreePath(new Object[]{model.getRoot()}));
	}
	
	private static class StatusBar extends JPanel implements LimeSpotTreeModel.Callback {

		private static final long	serialVersionUID	= 4154064262997413628L;
		
		private final JLabel text = new JLabel("...");
		StatusBar() {
			setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
			add(text);
		}
		public void setStatus(String msg) {
			text.setText(msg);
		}
		public void update(String msg) {
			setStatus(msg);
		}
	}
	
	public static void main(String[] args) {
		new Main().realMain(args);
	}

}
